package ${.BasePackageName}${.PackageName}.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ninelock.core.response.ResultMsg;
import com.ninelock.core.toolkit.QueryGenerator;
import ${.BasePackageName}${.PackageName}.entity.${.TableName | formatBigCamel};
import ${.BasePackageName}${.PackageName}.service.${.TableName | formatBigCamel}Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/** @author ${.Author} */
@Slf4j
@RestController
@RequestMapping("/${.TableName | formatSmallCamel}/")
public class ${.TableName | formatBigCamel}Controller {
  final ${.TableName | formatBigCamel}Service ${.TableName | formatSmallCamel}Service;

  public ${.TableName | formatBigCamel}Controller(${.TableName | formatBigCamel}Service ${.TableName | formatSmallCamel}Service) {
    this.${.TableName | formatSmallCamel}Service = ${.TableName | formatSmallCamel}Service;
  }

  @RequestMapping("getPage")
  public String getPage(
      ${.TableName | formatBigCamel} ${.TableName | formatSmallCamel},
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<${.TableName | formatBigCamel}> queryWrapper = QueryGenerator.initQueryWrapper(${.TableName | formatSmallCamel}, request.getParameterMap());
    Page<${.TableName | formatBigCamel}> pageable = new Page<>(page, size);
    IPage<${.TableName | formatBigCamel}> pageList = ${.TableName | formatSmallCamel}Service.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody ${.TableName | formatBigCamel} ${.TableName | formatSmallCamel}) {
    return ResultMsg.success(${.TableName | formatSmallCamel}Service.save(${.TableName | formatSmallCamel}));
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody ${.TableName | formatBigCamel} ${.TableName | formatSmallCamel}) {
    return ResultMsg.success(${.TableName | formatSmallCamel}Service.updateById(${.TableName | formatSmallCamel}));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
    return ResultMsg.success(${.TableName | formatSmallCamel}Service.getById(id));
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(${.TableName | formatSmallCamel}Service.removeById(id));
  }
}
